package com.example.smartcity.ui.guide;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.loginAdapter;
import com.example.smartcity.ui.Bean.loginList;
import com.example.smartcity.ui.Bean.logonBean;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class logon extends AppCompatActivity {

    String TAG = "TAG";
    ArrayList<loginList> list = new ArrayList<>();
    int[] icons = {R.drawable.phone,R.drawable.phone,R.drawable.phone,R.drawable.password};
    String[] hint = {"请输入账号","请输入昵称","请输入手机号码","请输入密码"};
    private ListView LV_logon;
    private Button btn_logon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logon);
        LV_logon = findViewById(R.id.LV_logon);
        btn_logon = findViewById(R.id.btn_logon);
        initView();
        setBtn_logon();
    }

    private void initView() {
        for (int i = 0; i < hint.length; i++) {
            loginList loginList = new loginList(hint[i],icons[i]);
            list.add(loginList);
        }
        LV_logon.setAdapter(new loginAdapter(logon.this,R.layout.loginitemlayout,list));

    }

    public void login(View view){
        Intent intent = new Intent(logon.this,login.class);
        startActivity(intent);
    }

    public void setBtn_logon(){
        btn_logon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String baseUrl = OkHttpUtils.getBaseUrl(logon.this) + "/system/user/register";
                //构建请求体
                logonBean logonBean = new logonBean();
                for (int i = 0; i < hint.length; i++) {
                    EditText editText = LV_logon.findViewWithTag(i);
                    switch (i){
                        case 0:
                            logonBean.setUserName(editText.getText().toString());
                            break;
                        case 1:
                            logonBean.setNickName(editText.getText().toString());
                            break;
                        case 2:
                            logonBean.setPhonenumber(editText.getText().toString());
                            break;
                        case 3:
                            logonBean.setPassword(editText.getText().toString());
                            break;
                    }
                }
                String json = new Gson().toJson(logonBean);
                RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=UTF-8"),json);
                OkHttpUtils.getCall(baseUrl,requestBody,new File(logon.this).readFile("Token")).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d(TAG, "onFailure: "+e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String json = response.body().string();
                        stateBean stateBean = new Gson().fromJson(json, com.example.smartcity.ui.Bean.stateBean.class);
                        int code = stateBean.getCode();
                        Looper.prepare();
                        if (code==200){
                            Toast.makeText(logon.this, "注册成功", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(logon.this, stateBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        Looper.loop();
                    }
                });
            }
        });
    }

}